package com.iims.project.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.iims.common.annotation.Log;
import com.iims.common.core.controller.BaseController;
import com.iims.common.core.domain.AjaxResult;
import com.iims.common.core.page.TableDataInfo;
import com.iims.common.enums.BusinessType;
import com.iims.project.domain.CmisWorkToken;
import com.iims.project.service.IWorkTokenService;
import com.iims.project.vo.query.GroupWorkTokenSearchQuery;
import com.iims.project.vo.request.WorkTokenEditRequest;
import com.iims.project.vo.resp.CmisWorkTokenRowVo;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @author fanpei
 * @date 2021/7/7 14:17
 */
@Api(tags = "J-工作令管理")
@RestController
@RequestMapping("/business/workToken")
public class WorkTokenController extends BaseController {

    @Autowired
    private IWorkTokenService workTokenService;

    /**
     * 获取工作令列表
     */
    @ApiOperation(value = "[高级搜索] 分页查询工作令列表", response = CmisWorkTokenRowVo.class)
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "当前记录起始索引(从1开始)", name = "pageNum", paramType = "query", required = true),
            @ApiImplicitParam(value = "每页显示记录数（>0）", name = "pageSize", paramType = "query", required = true),
            @ApiImplicitParam(value = "排序列", name = "orderByColumn", paramType = "query"),
            @ApiImplicitParam(value = "升降序方式：0升序 1降序", name = "isAsc", paramType = "query"),
            @ApiImplicitParam(value = "创建开始时间:format = \"yyyy-MM-dd\"", name = "beginTime", paramType = "query"),
            @ApiImplicitParam(value = "创建结束时间:format = \"yyyy-MM-dd\"", name = "endTime", paramType = "query"),
            @ApiImplicitParam(value = "管理类型:值来源于字典business_workToken_manageType", name = "manageType", paramType = "query"),
            @ApiImplicitParam(value = "研制方式,值来源于字典business_workToken_researchType", name = "researchType", paramType = "query"),
            @ApiImplicitParam(value = "执行状态,值来源于字典business_workToken_exeStatus", name = "exeStatus", paramType = "query"),
            @ApiImplicitParam(value = "归集状态,值来源于字典business_common_collectStatus", name = "collectStatus", paramType = "query"),
            @ApiImplicitParam(value = "业务类别,值来源于字典business_common_businessType", name = "businessType", paramType = "query"),
            @ApiImplicitParam(value = "产品频段,值来源于business_common_productBandType", name = "productBandType", paramType = "query"),
            @ApiImplicitParam(value = "工作令名(作令内容):模糊匹配", name = "workContent", paramType = "query"),
            @ApiImplicitParam(value = "工作令编号:模糊匹配", name = "workTokenCode", paramType = "query"),
            @ApiImplicitParam(value = "责任人:模糊匹配", name = "userChargeCN", paramType = "query")
    })
    @PreAuthorize("@ss.hasPermi('business:workToken:list')")
    @GetMapping("/list")
    public TableDataInfo list(@ApiIgnore CmisWorkToken workToken) throws Exception {
        startPage();
        setSearchTimeRange(workToken);
        setSeqNumPageSizeStart(workToken);
        return getDataTable(workTokenService.getWorkTokenList(workToken));
    }

    /**
     * [分组搜索] 分页查询工作令列表
     */
    @ApiOperation(value = "[分组搜索] 分页查询工作令列表", notes = "按钮分组顺序：scopeValue、exeStatus、businessType、timeValue", response = CmisWorkTokenRowVo.class)
    @ApiOperationSupport(order = 2)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "当前记录起始索引(从1开始)", name = "pageNum", paramType = "query", required = true),
            @ApiImplicitParam(value = "每页显示记录数（>0）", name = "pageSize", paramType = "query", required = true),
            @ApiImplicitParam(value = "排序列", name = "orderByColumn", paramType = "query"),
            @ApiImplicitParam(value = "升降序方式：0升序 1降序", name = "isAsc", paramType = "query"),
            @ApiImplicitParam(value = "综合模糊搜索:工作令内容/工作令编号/负责人/产品名称/产品频段", name = "searchValue", paramType = "query")
    })
    @PreAuthorize("@ss.hasPermi('business:workToken:list')")
    @GetMapping("/groupList")
    public TableDataInfo groupList(GroupWorkTokenSearchQuery groupSearchQuery) throws Exception {
        startPage();
        setSeqNumPageSizeStart(groupSearchQuery);
        return getDataTable(workTokenService.getWorkTokenListByGroup(groupSearchQuery));
    }

    @ApiOperation(value = "获取 [项目] 所属工作令列表", response = CmisWorkTokenRowVo.class)
    @GetMapping("/listByPro/{proId}")
    public AjaxResult listByPro(@ApiParam("项目Id") @PathVariable Long proId) {
        return AjaxResult.success(workTokenService.getProWorkTokenList(proId));
    }

    @ApiOperation(value = "获取 [合同] 所属工作令列表", response = CmisWorkTokenRowVo.class)
    @GetMapping("/listByContract/{contractId}")
    public AjaxResult listByContract(@ApiParam("正式合同Id") @PathVariable Long contractId) {
        return AjaxResult.success(workTokenService.getContractWorkTokenList(contractId));
    }

    /**
     * 根据工作令编号获取详细信息
     */
    @ApiOperation(value = "根据工作令Id获取详细信息", response = CmisWorkTokenRowVo.class)
    @ApiImplicitParam(name = "workTokenId", value = "工作令Id", paramType = "path", required = true)
    @PreAuthorize("@ss.hasPermi('business:workToken:query')")
    @GetMapping(value = "/{workTokenId}")
    public AjaxResult getInfo(@PathVariable Long workTokenId) {
        return AjaxResult.success(workTokenService.getWorkTokenDetailById(workTokenId));
    }

    /**
     * 新增工作令
     */
    @ApiOperation(value = "新增工作令")
    @ApiOperationSupport(ignoreParameters = {"workTokenId", "workTokenCode", "exeStatus", "userChargeCN", "deptChargeCN"})
    @PreAuthorize("@ss.hasPermi('business:workToken:add')")
    @Log(title = "新增工作令", businessType = BusinessType.INSERT)
    @Transactional
    @PostMapping
    public AjaxResult add(@Validated @RequestBody WorkTokenEditRequest workToken) {
        workTokenService.insertWorkToken(workToken);
        return AjaxResult.success();
    }

    /**
     * 修改工作令
     */
    @ApiOperation("修改工作令")
    @PreAuthorize("@ss.hasPermi('business:workToken:edit')")
    @ApiOperationSupport(ignoreParameters = {"workTokenCode", "exeStatus", "userChargeCN", "deptChargeCN"})
    @Log(title = "修改工作令", businessType = BusinessType.UPDATE)
    @Transactional
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody WorkTokenEditRequest workToken) {
        workTokenService.updateWorkToken(workToken);
        return AjaxResult.success();
    }

    /**
     * 删除工作令
     */
    @ApiOperation("删除工作令")
    @PreAuthorize("@ss.hasPermi('business:workToken:remove')")
    @Log(title = "删除工作令", businessType = BusinessType.DELETE)
    @Transactional
    @DeleteMapping("/{workTokenId}")
    public AjaxResult remove(@ApiParam("工作令Id") @PathVariable Long workTokenId) {
        if (workTokenService.checkCmisWorkTokenUse(workTokenId)) {
            return AjaxResult.error("删除工作令失败，工作令已被使用！");
        }
        workTokenService.deleteById(workTokenId);
        return AjaxResult.success();
    }
}
